/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 15, 2012  10:28:41 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : CostArticleGroupAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;
import jpa.entitys.promis.common.CostArticleGroup;
import jpa.entitys.synchro.staging.MmMatmasInb;
import jpa.service.promis.common.CostArticleGroupServices;
import jpa.service.synchro.staging.MmMatmasInbServices;
import org.apache.log4j.Logger;
import promis.utility.ACTION_HANDLER;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:31:18 PM 
 * CreateBy=Chatree Siangklom
 * 
 * CostArticleGroupAction
 * TODO
 */
public class CostArticleGroupAction {
	
	private static final Logger log = Logger.getLogger(CostArticleGroupAction.class);
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:31:03 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleGroupAction
	 * TODO
	 */
	public CostArticleGroupAction() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:31:06 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleGroup
	 * TODO
	 * @param ArticleGroupCode
	 * @return
	 * @throws Exception
	 */
	public CostArticleGroup FindByKey(String ArticleGroupCode) throws Exception {
		
		CostArticleGroupServices costArticleGroupSrv = new CostArticleGroupServices(sessionFactory.CreateEmPromis());		
		
		return costArticleGroupSrv.findById(ArticleGroupCode);
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:31:10 PM 
	 * CreateBy=Chatree Siangklom
	 * boolean
	 * TODO
	 * @param ArticleGroupCode
	 * @return
	 * @throws Exception
	 */
	public boolean ExistingChecked(String ArticleGroupCode) throws Exception {
		return FindByKey(ArticleGroupCode) != null ? true : false; 
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:31:14 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleGroup
	 * TODO
	 * @param mmMatmasInb
	 * @return
	 * @throws Exception
	 */
	public CostArticleGroup SaveOrUpdateArticleGroup(MmMatmasInb mmMatmasInb) throws Exception {
		
		try{
			CostArticleGroupServices costArticleGroupSrv = new CostArticleGroupServices(sessionFactory.CreateEmPromis());
			CostArticleGroup model = FindByKey(mmMatmasInb.getArtiGrpCode());
			if( model == null){			
				//Inserted
				
				System.out.println("Insert CostArticleGroup.");
				log.info("Insert CostArticleGroup.");
				model = new CostArticleGroup();
				model.setArticleGroup(mmMatmasInb.getArtiGrpCode());
				model.setDescription(mmMatmasInb.getArtiGrpName());
				model.setArticleType(mmMatmasInb.getArtiTypeCode());
				model.setSyncIssync("N");
				
				model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));
				model.setCreatedBy(mmMatmasInb.getCreatedBy());
				
				return costArticleGroupSrv.InsertedOrUpdated(model, ACTION_HANDLER.INSERTED);
			}else
			{
				//Updated
				System.out.println("Updated CostArticleGroup.");
				log.info("Updated CostArticleGroup.");
				model.setDescription(mmMatmasInb.getArtiGrpName());
				model.setArticleType(mmMatmasInb.getArtiTypeCode());
				model.setSyncIssync("N");
				
				if(CommonService.IsNull(mmMatmasInb.getUpdatedOn()))
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));				
				else
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getUpdatedOn()));
				
				model.setLastUpdBy(CommonService.IsNullRNull(mmMatmasInb.getCreatedBy()));
				
				return costArticleGroupSrv.InsertedOrUpdated(model, ACTION_HANDLER.UPDATED);	
			}	
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());		
			throw e;
		}
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:31:23 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	private void testedd () throws Exception {
		
		MmMatmasInbServices obj = new MmMatmasInbServices(sessionFactory.CreateEmSynchro());
		List<MmMatmasInb> lst =  obj.findAllIsSync("N");
		log.info("lst.size : " +lst.size());
		
		for(MmMatmasInb mmMatmasInb : lst){
			try{
				//log.info("ExistingChecked : " +ExistingChecked(mmMatmasInb.getArtiGrpCode()));
				//log.info("mmMatmasInb.getArtiGrpCode : " + mmMatmasInb.getArtiGrpCode());
				
				SaveOrUpdateArticleGroup(mmMatmasInb);
				
				mmMatmasInb.setIssync("Y");
				mmMatmasInb.setMsgCode("S");
				mmMatmasInb.setMsgDesc("Success");
				mmMatmasInb.setMsgActResult(null);
				obj.Updated(mmMatmasInb);
				
			}catch(Exception e){
				mmMatmasInb.setIssync("N");
				mmMatmasInb.setMsgCode("E");
				mmMatmasInb.setMsgDesc("Error");
				mmMatmasInb.setMsgActResult(e.getMessage());
				obj.Updated(mmMatmasInb);
			}
		}
	}
	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		
//		CostArticleGroupAction xx = new CostArticleGroupAction();
//		try {
//			
//			xx.testedd();
//			
//			
//			CostArticleGroup obj = xx.FindByKey("727");
//			log.info("getDescription : " + obj.getDescription());			
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

}
